In this project, I will use “Insectsprays” data to create a plot using plotly. The data set Insectsprays gives the counts of insects in agricultural experimental units treated with different insecticides.
Loading required packages:
library(plotly)
## Loading required package: ggplot2
##
## Attaching package: 'plotly'
## The following object is masked from 'package:ggplot2':
##
## last_plot
## The following object is masked from 'package:stats':
##
## filter
## The following object is masked from 'package:graphics':
##
## layout
data("InsectSprays")
summary(InsectSprays)
## count spray
## Min. : 0.00 A:12
## 1st Qu.: 3.00 B:12
## Median : 7.00 C:12
## Mean : 9.50 D:12
## 3rd Qu.:14.25 E:12
## Max. :26.00 F:12
trend <- aggregate(count~spray, data=InsectSprays, mean)
fit <- lm(count~spray, data= trend)
plot_ly(trend, x=~spray, y=~count, type="scatter", mode = 'markers') %>% add_lines(x=~spray, y=fitted(fit))%>% layout(title="The Relationship between the Insect Sprays and Insect Count")